﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using AutoMapper;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;

namespace KernelWorkflow.Activities
{
    public partial class GetTeacherDaySchedule : SequenceActivity
    {
        private bool crutch_first_time = true;
        public static readonly string GetTeacherDayScheduleAccessObject = "GetTeacherDaySchedule";

        //in
        public static DependencyProperty CurrentSessionProperty = DependencyProperty.Register("CurrentSession", typeof(SessionDto), typeof(GetTeacherDaySchedule));
        public static DependencyProperty CurrentTeacherGuidProperty = DependencyProperty.Register("CurrentTeacherGuid", typeof(Guid), typeof(GetTeacherDaySchedule));
        public static DependencyProperty CurrentDayInAWeekProperty = DependencyProperty.Register("CurrentDayInAWeek", typeof(DateTime), typeof(GetTeacherDaySchedule));
        //out
        public static DependencyProperty TeacherDayScheduleProperty = DependencyProperty.Register("TeacherDaySchedule", typeof(TeacherDayFullDataScheduleDto), typeof(GetTeacherDaySchedule));

        [Category("AUTHORIZATION_INPUT")]
        [Browsable(true)]
        public SessionDto CurrentSession
        {
            get
            {
                return ((SessionDto)(base.GetValue(GetTeacherDaySchedule.CurrentSessionProperty)));
            }
            set
            {
                base.SetValue(GetTeacherDaySchedule.CurrentSessionProperty, value);
            }
        }

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public Guid CurrentTeacherGuid
        {
            get
            {
                return ((Guid)(base.GetValue(GetTeacherDaySchedule.CurrentTeacherGuidProperty)));
            }
            set
            {
                base.SetValue(GetTeacherDaySchedule.CurrentTeacherGuidProperty, value);
            }
        }

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public DateTime CurrentDayInAWeek
        {
            get
            {
                return ((DateTime)(base.GetValue(GetTeacherDaySchedule.CurrentDayInAWeekProperty)));
            }
            set
            {
                base.SetValue(GetTeacherDaySchedule.CurrentDayInAWeekProperty, value);
            }
        }


        [Category("DATA_OUTPUT")]
        [Browsable(true)]
        public TeacherDayFullDataScheduleDto TeacherDaySchedule
        {
            get
            {
                return ((TeacherDayFullDataScheduleDto)(base.GetValue(GetTeacherDaySchedule.TeacherDayScheduleProperty)));
            }
            set
            {
                base.SetValue(GetTeacherDaySchedule.TeacherDayScheduleProperty, value);
            }
        }


        public Boolean isSessionOk = false;

        private void GetTeacherDaySchedule_ExecuteCode(object sender, EventArgs e)
        {
            if (crutch_first_time)
            {
                Mapper.CreateMap<TeacherLineScheduleDto, TeacherLineWSScheduleDto>();
                this.crutch_first_time = false;
            }

            Console.WriteLine("GetTeacherDaySchedule::GetTeacherDaySchedule_ExectuteCode");

            AuthorizationService.AuthorizationClient authservice = new AuthorizationService.AuthorizationClient();
            isSessionOk = authservice.CheckRights(CurrentSession, GetTeacherDayScheduleAccessObject);
            authservice.Close();
            Console.WriteLine("isSessionOk = {0}", isSessionOk);

            if (isSessionOk)
            {
                TeacherDaySchedule = new TeacherDayFullDataScheduleDto();
                List<TeacherLineWSScheduleDto> lines = new List<TeacherLineWSScheduleDto>();
                TeacherDaySchedule.Lines = lines;
                TeacherDaySchedule.RequestedDay = CurrentDayInAWeek;

                ScheduleService.SchedulingServiceClient scheduleclient = new ScheduleService.SchedulingServiceClient();
                scheduleclient.Open();
                TeacherDayScheduleDto dockday = scheduleclient.GetTeacherScheduleForADay(CurrentSession, CurrentTeacherGuid, CurrentDayInAWeek);
                scheduleclient.Close();

                HrService.HrServiceClient hrclient = new HrService.HrServiceClient();
                hrclient.Open();
                DirectorateService.DirectorateServiceClient directorateclient = new DirectorateService.DirectorateServiceClient();
                directorateclient.Open();

                if (dockday != null && dockday.Lines != null)
                {
                    TeacherDaySchedule.Teacher = hrclient.GetTeacher(CurrentSession, dockday.TeacherGuid);
                    IEnumerator<TeacherLineScheduleDto> docklineEnumerator = dockday.Lines.GetEnumerator();

                    while (docklineEnumerator.MoveNext())
                    {
                        TeacherLineWSScheduleDto line = Mapper.Map<TeacherLineScheduleDto, TeacherLineWSScheduleDto>(docklineEnumerator.Current);
                        line.Subject = directorateclient.GetSubject(CurrentSession, docklineEnumerator.Current.SubjectGuid);
                        lines.Add(line);
                    }
                }

                hrclient.Close();
                directorateclient.Close();
            }
        }


        public GetTeacherDaySchedule()
        {
            InitializeComponent();
        }
    }
}
